﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.ServiceModel;
using System.Collections.ObjectModel;
using FluidKit.Controls;


namespace LauncherWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //private int _currentMenuLevel = 0;
        private int _parentMenuLevel = 0;
        private MenuLevel _currentMenu = null;
        private const int _visibleWindow = 20;
        private int _currentIndex = -1;
        private bool _completeLoad = false;
        private int _minSourceIndex = -1;
        private int _maxSourceIndex = -1;


        private ObservableCollection<MenuLink> _links = new ObservableCollection<MenuLink>();

        public MainWindow()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainWindow_Loaded);


        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
     
            _elementFlow.ItemsSource = _links;
           // Mouse.OverrideCursor = Cursors.None;
            getMenuLevel(0);
        }



        public void getMenuLevel(int level)
        {
            debugText.Text = "getMenuLevel for level " + level;

           // _currentMenuLevel = level;

            for(int x = _links.Count-1; x >= 0; x--)
                _links.RemoveAt(x);


            
                
            _currentMenu = ClientLib.getMenuLevel(level);

            debugText.Text += "[" + _currentMenu.Links.Count.ToString() + "]";

            _parentMenuLevel = _currentMenu.ParentMenuId;

            if (_currentMenu.Links.Count > _visibleWindow)
            {
                for (int i = 0; i < _visibleWindow; i++)
                {
                    _links.Add(_currentMenu.Links[i]);
                }
                _completeLoad = false;
            }
            else
            {
                // load the whole lot
                _completeLoad = true;
                _minSourceIndex = 0;
                _maxSourceIndex = _visibleWindow - 1;
                foreach (MenuLink link in _currentMenu.Links)
                {
                    _links.Add(link);
                }
            }

           // _elementFlow.UpdateLayout();

            //_elementFlow.ItemsSource = null;
            //_elementFlow.ItemsSource = _links;
            _currentIndex = 0;
            _elementFlow.SelectedIndex = 0;
            _elementFlow.Focus();
         
        }

        public void launchGame(int gameId)
        {
            ClientLib.LaunchGame(gameId);
        }

        private void Image_MouseUp(object sender, MouseButtonEventArgs e)
        {
            //MessageBox.Show("hello");
        }

        private void _elementFlow_KeyDown(object sender, KeyEventArgs e)
        {




            if (e.Key == Key.D1)
            {
                int index = _elementFlow.SelectedIndex;
                //_previousMenuLevel = _currentMenuLevel;

                int key = _links[index].Key;
                if (_links[index].IsMenu)
                {
                    // launch menu
                    getMenuLevel(key);
                }
                else
                {
                    launchGame(key);
                }

                //MessageBox.Show(name + "key pressed");

            }
            else if (e.Key == Key.D2)
            {
                getMenuLevel(_parentMenuLevel);
            }
            else if (e.Key == Key.Escape)
            {
                this.Close();
            }
        }

        private void _elementFlow_SelectedIndexChanged(object sender, EventArgs e)
        {
            // compare previous index
            int newIndex = ((ElementFlow)sender).SelectedIndex;
            if (!_completeLoad)
            {
                // if moved to right, need to load onto end
                if (newIndex > _currentIndex && !((_maxSourceIndex+1) >= _currentMenu.Links.Count))
                {
                    _links.Add(_currentMenu.Links[++_maxSourceIndex]);
                    // need to delete first one
                }
            }

            _currentIndex = newIndex;
            
        }

        
    }




}
